﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using ProjectWizardManagement.Entity.Model;
using ProjectWizardManagementv1.Infrastructure.Static;

namespace ProjectWizardManagementv1.Infrastructure.Concrete
{
    public class FileSystemFactory
    {
        public static ProjectFile ConstructFileHierarchy(string file )
        {
            var fileName = Path.GetFileName(file);
            ProjectFile projectFile = null;
            if((File.Exists(file) || Directory.Exists(file)) && fileName != null)
            {
                try
                {
                    projectFile = new ProjectFile
                    {
                        Path = file,
                        Extension = Path.GetExtension(file),
                        Name = Path.GetFileNameWithoutExtension(file)
                    };
                    FileAttributes attributes = File.GetAttributes(file);
                    projectFile.IsDirectory = ((attributes & FileAttributes.Directory) == FileAttributes.Directory);
                    if (projectFile.IsDirectory)
                        projectFile.ProjectFiles = new ObservableCollection<ProjectFile>(ConstructFileHierarchy(MyDirectory.EnumerateFilesAndDirectories(file)));  
                }
                catch (Exception)
                {

                }
            }

            return projectFile;
        }

        public static List<ProjectFile> ConstructFileHierarchy(IEnumerable<string> files)
        {
            return files.Select(ConstructFileHierarchy).Where(f => f != null).ToList();
        }
    }
}
